package com.salesmanager.web.utils;

import com.salesmanager.core.business.content.model.FileContentType;
import com.salesmanager.core.utils.CoreConfiguration;
import com.salesmanager.core.utils.SpringContextUtils;
import com.salesmanager.web.common.ImageSize;
import com.salesmanager.web.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class ImageFilePathUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(ImageFilePathUtils.class);


    /**
     *
     * @param code
     * @param imageName
     * @param size
     * @param request
     * @return
     */
    public static String buildAdImageFilePath(String code,String imageName,ImageSize size,HttpServletRequest request){
        StringBuilder path = new StringBuilder();
        path.append(getImage(request, size).getBasePath())
                .append(com.salesmanager.core.utils.ImageFilePathUtils.buildAdImageFilePath(code, imageName));
        return path.toString();
    }


    /**
     *
     * @param code
     * @param imageName
     * @param size
     * @param request
     * @return
     */

    public static String buildMessageImageFilePath(String code,String imageName,ImageSize size,HttpServletRequest request){
        StringBuilder path = new StringBuilder();
        path.append(getImage(request, size).getBasePath())
                .append(com.salesmanager.core.utils.ImageFilePathUtils.buildMessageImageFilePath(code,imageName));

        return path.toString();
    }


    /**
     * Android 渠道包下载地址
     * @param channelCode
     * @param fileName
     * @param request
     * @return
     */
    public static String buildChannelApkFilePath(String channelCode, String fileName, HttpServletRequest request){
        StringBuilder path = new StringBuilder();
        Image image = getImage(request, ImageSize.RAW);
        path.append(image.getBasePath())
                .append(com.salesmanager.core.utils.ImageFilePathUtils.buildChannelApkFilePath(channelCode, fileName));
        return path.toString();
    }


    /**
     * 应用图片文件路径
     * @param appCode
     * @param imageName
     * @return
     */
    public static String buildAppImageFilePath(String appCode, String imageName,HttpServletRequest request) {
        StringBuilder path = new StringBuilder();
        Image image = getImage(request, ImageSize.RAW);
        path.append(image.getBasePath())
                .append(com.salesmanager.core.utils.ImageFilePathUtils.buildAppImageFilePath(appCode, imageName));
        return path.toString();
    }


    /**
     * 应用安装包文件路径
     *
     * @param appCode
     * @param fileName
     * @return
     */
    public static String buildAppApKFilePath(String appCode, String fileName,HttpServletRequest request) {
        StringBuilder path = new StringBuilder();
        Image image = getImage(request, ImageSize.RAW);
        path.append(image.getBasePath())
                .append(com.salesmanager.core.utils.ImageFilePathUtils.buildAppApKFilePath(appCode, fileName));
        return path.toString();
    }


    /**
     *
     * @param request
     * @return
     */
    private static Image getImage(HttpServletRequest request, ImageSize size){
        Image image = new Image();
        //基础路径
        StringBuilder path = new StringBuilder();

        //图片尺寸规则
        String imageSizeRule = "";

        CoreConfiguration configuration = (CoreConfiguration)SpringContextUtils.getBean("coreConfiguration");
        //AliYun OSS
        if (configuration != null && configuration.getProperty(com.salesmanager.core.constants.Constants.CMS_SERVER).equals(com.salesmanager.core.constants.Constants.CMS_SERVER_OSS)){
            path.append(configuration.getProperty(Constants.ALIYUN_OSS_IMG_DOMAIN));

            //图片尺寸规则
            if (size != null && !size.name().equals(ImageSize.RAW.name())){
                imageSizeRule = getImageSizeRule(size,configuration);
            }

        }else{
            //
            if (configuration == null){
                path.append("http://img.xm9m.com");
                LOGGER.warn("CoreConfiguration is null, Session id:{}",request.getSession().getId());
            } else {
                path.append(request.getContextPath());
            }
        }

        image.setBasePath(path.toString());
        image.setImageSizeRule(imageSizeRule);

        return image;
    }


    /**
     *
     * @param size
     * @param configuration
     * @return
     */
    private static String getImageSizeRule(ImageSize size,CoreConfiguration configuration){
        String imageSizeRule;
        if (null==size){
            return configuration.getProperty(Constants.ALIYUN_OSS_IMG_SIZE_NORMAL);
        }

        switch (size){
            case NORMAL:
                imageSizeRule = configuration.getProperty(Constants.ALIYUN_OSS_IMG_SIZE_NORMAL);
                break;
            case SMALL:
                imageSizeRule = configuration.getProperty(Constants.ALIYUN_OSS_IMG_SIZE_SM);
                break;
            case XSMALL:
                imageSizeRule = configuration.getProperty(Constants.ALIYUN_OSS_IMG_SIZE_XS);
                break;
            case RAW:
                imageSizeRule = "";
                break;
            default:
                imageSizeRule = configuration.getProperty(Constants.ALIYUN_OSS_IMG_SIZE_NORMAL);
                break;
        }

        return imageSizeRule;
    }

    static class Image{
        private String basePath;
        private String imageSizeRule;

        public String getBasePath() {
            return basePath;
        }

        public void setBasePath(String basePath) {
            this.basePath = basePath;
        }

        public String getImageSizeRule() {
            return imageSizeRule;
        }

        public void setImageSizeRule(String imageSizeRule) {
            this.imageSizeRule = imageSizeRule;
        }
    }


}
